package li202002.fg.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class FG0Crypt {
    static char[] HEX_CHARS;
    private String SecretKey;
    private Cipher cipher;
    private String iv;
    private IvParameterSpec ivspec;
    private SecretKeySpec keyspec;

    static {
        FG0Crypt.HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    }

    public FG0Crypt(String arg3, String arg4, String arg5) {
        super();
        StringBuilder v0 = new StringBuilder();
        v0.append(arg3);
        v0.append(arg4);
        this.iv = FG0Crypt.md5(v0.toString()).substring(0, 16);
        StringBuilder v4 = new StringBuilder();
        v4.append(arg3);
        v4.append(arg5);
        this.SecretKey = FG0Crypt.md5(v4.toString());
        this.ivspec = new IvParameterSpec(this.iv.getBytes());
        this.keyspec = new SecretKeySpec(this.SecretKey.getBytes(), "AES");
        try {
            this.cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
        }
        catch(NoSuchPaddingException v3) {
            v3.printStackTrace();
        }
        catch(NoSuchAlgorithmException v3_1) {
            v3_1.printStackTrace();
        }
    }

    private static String bytesToHex(byte[] arg5) {
        char[] v0 = new char[arg5.length * 2];
        int v1;
        for(v1 = 0; v1 < arg5.length; ++v1) {
            int v2 = v1 * 2;
            char[] v3 = FG0Crypt.HEX_CHARS;
            v0[v2] = v3[(arg5[v1] & 0xF0) >>> 4];
            v0[v2 + 1] = v3[arg5[v1] & 15];
        }

        return new String(v0);
    }

    public byte[] decrypt(String arg5) throws Exception {
        byte[] v5_1;
        if(arg5 != null && arg5.length() != 0) {
            try {
                this.cipher.init(2, this.keyspec, this.ivspec);
                v5_1 = this.cipher.doFinal(FG0Crypt.hexToBytes(arg5));
                if(v5_1.length > 0) {
                    int v0 = v5_1.length - 1;
                    int v2 = 0;
                    while(v0 >= 0) {
                        if(v5_1[v0] == 0) {
                            ++v2;
                        }

                        --v0;
                    }

                    if(v2 <= 0) {
                        return v5_1;
                    }

                    byte[] v0_1 = new byte[v5_1.length - v2];
                    System.arraycopy(v5_1, 0, v0_1, 0, v5_1.length - v2);
                    v5_1 = v0_1;
                }
            }
            catch(Exception v5) {
                StringBuilder v1 = new StringBuilder();
                v1.append("[decrypt] ");
                v1.append(v5.getMessage());
                throw new Exception(v1.toString());
            }

            return v5_1;
        }

        throw new Exception("Empty string");
    }

    public String decryptString(String arg2) {
        if(arg2 == null) {
            return null;
        }

        if(arg2.startsWith("{")) {
            return arg2;
        }

        try {
            return new String(this.decrypt(arg2));
        }
        catch(Exception v2) {
            v2.printStackTrace();
            return v2.toString();
        }
    }

    public byte[] encrypt(String arg5) throws Exception {
        if(arg5 != null && arg5.length() != 0) {
            try {
                this.cipher.init(1, this.keyspec, this.ivspec);
                return this.cipher.doFinal(FG0Crypt.padString(arg5).getBytes());
            }
            catch(Exception v5) {
                StringBuilder v1 = new StringBuilder();
                v1.append("[encrypt] ");
                v1.append(v5.getMessage());
                throw new Exception(v1.toString());
            }
        }

        throw new Exception("Empty string");
    }

    public String encryptString(String arg1) {
        try {
            return FG0Crypt.bytesToHex(this.encrypt(arg1));
        }
        catch(Exception v1) {
            return v1.toString();
        }
    }

    private static byte[] hexToBytes(String arg5) {
        byte[] v0 = null;
        if(arg5 == null) {
            return v0;
        }

        int v2 = 2;
        if(arg5.length() < v2) {
            return v0;
        }

        int v0_1 = arg5.length() / v2;
        byte[] v1 = new byte[v0_1];
        for(v2 = 0; v2 < v0_1; ++v2) {
            int v3 = v2 * 2;
            v1[v2] = ((byte)Integer.parseInt(arg5.substring(v3, v3 + 2), 16));
        }

        return v1;
    }

    public static boolean isValidMD5(String arg1) {
        return arg1.matches("^[a-fA-F0-9]{32}$");
    }

    private static String md5(String arg7) {
        try {
            MessageDigest v0 = MessageDigest.getInstance("MD5");
            v0.update(arg7.getBytes());
            byte[] v7_1 = v0.digest();
            StringBuilder v0_1 = new StringBuilder();
            int v1 = v7_1.length;
            int v3;
            for(v3 = 0; v3 < v1; ++v3) {
                StringBuilder v5 = new StringBuilder(Integer.toHexString(v7_1[v3] & 0xFF));
                while(v5.length() < 2) {
                    v5.insert(0, "0");
                }

                v0_1.append(((CharSequence)v5));
            }

            return v0_1.toString();
        }
        catch(NoSuchAlgorithmException v7) {
            v7.printStackTrace();
            return "";
        }
    }

    private static String padString(String arg3) {
        int v0 = 16 - arg3.length() % 16;
        StringBuilder v1 = new StringBuilder(arg3);
        int v2;
        for(v2 = 0; v2 < v0; ++v2) {
            v1.append('\u0000');
        }

        return v1.toString();
    }
}

